/*
* PureMVC Logger v1.0
* Copyright (c) 2009 We Are Cloud.
* Author: Yannick Chaze
*
* Based on PureMVC code
* See http://puremvc.org for more informations
*
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0.
*
* PureMVC MultiCore - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
* Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/
package com.wearecloud.puremvclogger.core
{
	import com.wearecloud.puremvclogger.Logger;
	import com.wearecloud.puremvclogger.patterns.observer.ObserverLogger;
	
	import org.puremvc.as3.multicore.core.Controller;
	import org.puremvc.as3.multicore.interfaces.IController;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	/**
	 * This class extends the <code>Controller</code> class of PureMVC
	 * but use the <code>ViewController</code> class instead of
	 * the original <code>View</code> class
	 * 
	 * Enable the framework to work properly with the logger class
	 * 
	 * @see org.puremvc.as3.multicore.core.Controller
	 * @see org.puremvc.as3.multicore.interfaces.IController
	 */ 
	public class ControllerLogger extends Controller implements IController
	{
		/**
		 * @inheritDoc
		 */
		public function ControllerLogger ( key:String )
		{
			super( key );
		}
		
		/**
		 * @inheritDoc
		 */
		public static function getInstance( key:String ) : IController
		{
			if ( instanceMap[ key ] == null ) instanceMap[ key ] = new ControllerLogger( key );
			return instanceMap[ key ];
		}
		
		/**
		 * @inheritDoc
		 */
		override public function registerCommand( notificationName : String, commandClassRef : Class ) : void
		{
			if ( commandMap[ notificationName ] == null ) 
			{
				view.registerObserver( notificationName, new ObserverLogger( executeCommand, this ) );
			}
			commandMap[ notificationName ] = commandClassRef;
		}
		
		/**
		 * @inheritDoc
		 */
		public function getCommandName ( notificationName:String):Class
		{
			return commandMap [ notificationName ];
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function initializeController(  ) : void 
		{
			view = ViewLogger.getInstance( multitonKey );
		}
		
		/**
		 * @inheritDoc
		 */
		public static function removeController( key:String ):void
		{
			delete instanceMap[ key ];
		}
	}
}